package demoMod.stsconsole.rooms;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.dungeons.AbstractDungeon;
import demoMod.stsconsole.events.AbstractEvent;
import demoMod.stsconsole.helpers.PromptHelper;
import demoMod.stsconsole.interfaces.DropRewardSubscriber;
import demoMod.stsconsole.rewards.CardReward;
import demoMod.stsconsole.rewards.GoldReward;
import demoMod.stsconsole.rewards.PotionReward;

public class EventRoom extends AbstractRoom {
    private AbstractEvent event;

    public EventRoom() {
        this.phase = RoomPhase.UNCOMPLETED;
    }

    @Override
    public void onPlayerEntry() {
        super.onPlayerEntry();
        if (this.phase != RoomPhase.COMPLETED) {
            int treasureChance = AbstractDungeon.eventMonsterBlizzardCount + AbstractDungeon.eventChestBlizzardCount;
            int shopChance = treasureChance + AbstractDungeon.eventShopBlizzardCount;
            int roll = AbstractDungeon.eventRng.nextInt(100);
            if (roll < AbstractDungeon.eventMonsterBlizzardCount) {
                this.phase = RoomPhase.COMBAT;
                this.enterCombat(AbstractDungeon.currDungeon.getEncounter(this));
                AbstractDungeon.eventMonsterBlizzardCount = 0;
            } else if (roll < treasureChance) {
                this.phase = RoomPhase.COMPLETED;
                this.chest = AbstractDungeon.getRandomChest();
                AbstractDungeon.eventChestBlizzardCount = -3;
            } else if (roll < shopChance) {
                //TODO generate shop
                this.phase = RoomPhase.COMPLETED;
                AbstractDungeon.eventShopBlizzardCount = -5;
                AbstractDungeon.currDungeon.currMapNode.room = new ShopRoom();
                AbstractDungeon.currDungeon.currMapNode.room.onPlayerEntry();
            } else {
                this.event = AbstractDungeon.currDungeon.getEvent();
                AbstractDungeon.currEventId = event.id;
                event.updateBodyText();
            }
            AbstractDungeon.eventMonsterBlizzardCount += 10;
            AbstractDungeon.eventChestBlizzardCount += 3;
            AbstractDungeon.eventShopBlizzardCount += 5;
        } else {
            if (!AbstractDungeon.currEventId.equals("")) {
                this.event = AbstractDungeon.currDungeon.getEventInCurrDungeon(AbstractDungeon.currEventId);
                PromptHelper.backwardPrompt();
            }
            dropReward();
        }
    }

    @Override
    public String getRoomSymbol() {
        return "?";
    }

    public AbstractEvent getEvent() {
        return event;
    }

    @Override
    public void dropReward() {
        if (event != null) event.dropReward(this, this.shouldDropReward);
        if (this.shouldDropReward) {
            this.rewards.add(new CardReward());
            this.rewards.add(new GoldReward(this.goldAmount));
            if (AbstractDungeon.potionRng.nextBoolean(0.2F)) {
                this.rewards.add(new PotionReward());
            }
        }
        ConsoleEntry.triggerSubscribers(DropRewardSubscriber.class, "onDropReward",
                new Class[]{boolean.class}, new Object[]{this.shouldDropReward});
    }
}
